import Vue from 'vue'

const util = {
    /**
     * 传入一个对象，对象的属性作为参数键，对象属性的值作为参数值，返回对应FormData
     * @param paramObj
     */
    formDataFactory(paramObj) {
        if (!paramObj) {
            paramObj = {}
        }
        let data = new FormData()
        Object.keys(paramObj).forEach((key) => data.append(key, paramObj[key]))
        return data
    },
    //获取当前时间 格式：2013-01-01  //00:00:00
    curentTime() {
        var now = new Date()

        var year = now.getFullYear()
        var month = now.getMonth() + 1
        var day = now.getDate()

        var hh = now.getHours()
        var mm = now.getMinutes()

        var clock = year + '-'

        if (month < 10) clock += '0'

        clock += month + '-'

        if (day < 10) clock += '0'

        clock += day + ' '

        /* if (hh < 10) clock += "0";

        clock += hh + ":";
        if (mm < 10) clock += "0";
        clock += mm; */
        return clock
    },

    /**
     * 计算两个日期之间的天数
     * @param dateString1  开始日期 yyyy-MM-dd
     * @param dateString2  结束日期 yyyy-MM-dd
     * @param type   days 纯转换天数 hours小时
     * @returns {number} 如果日期相同 返回一天 开始日期大于结束日期，返回0
     */
    getDays(dateString1, dateString2, type) {
        const startDate = new Date(dateString1.replace(/-/g, '/').replace(/T/, ' ')).getTime()
        const endDate = new Date(dateString2.replace(/-/g, '/').replace(/T/, ' ')).getTime()

        if (startDate >= endDate) {
            return 0
        }
        const days = endDate - startDate
        //计算出相差天数
        const day = Math.floor(days / (24 * 3600 * 1000))
        //计算出小时数
        const leave1 = days % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
        const hours = Math.floor(leave1 / (3600 * 1000))
        //计算相差分钟数
        const leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
        const minutes = Math.floor(leave2 / (60 * 1000))
        //计算相差秒数
        const leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
        const seconds = Math.round(leave3 / 1000)
        // alert(" 相差 "+days+"天 "+hours+"小时 "+minutes+" 分钟"+seconds+" 秒")
        if (type === 'days') {
            return (days / (24 * 3600 * 1000)).toFixed(1)
        }
        if (type === 'hours') {
            return ((days / (24 * 3600 * 1000)) * 24).toFixed(1)
        }
        if (type === 'minutes') {
            return ((days / (24 * 3600 * 1000) / 24) * 3600).toFixed(1)
        }

        //全部输出
        let str = ''
        if (day) {
            str += day + '天'
        }
        if (hours) {
            str += hours + '时'
        }
        if (minutes) {
            str += minutes + '分'
        }

        return str
    },
    //获取url参数
    getUrlParams(variable) {
        var query = window.location.search.substring(1)
        var vars = query.split('&')
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split('=')
            if (pair[0] == variable) {
                return pair[1]
            }
        }
        return false
    },
    isIos() {
        var u = navigator.userAgent
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 //android终端
        if (isAndroid) {
            return false
        }
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) //ios终端
        if (isiOS) {
            return true
        }
        return false
    },
}
export default util
Vue.prototype.$utils = Vue.prototype.$util = util
